<?php
namespace app\api\controller;

use app\api\basic\BaseController;
use app\cassification\logic\ClassificationDictionaryLogic;
use app\livebroadcast\logic\liveBroadcastLogic;
use plugin\saiadmin\app\model\system\SystemDictData;
use app\api\annotation\LoginRequired;
use support\Request;
use support\Response;

class livebroadcastController extends BaseController
{
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
    /**
     * 直播分类
     */
    #[LoginRequired(required:false)]
    public function  livebroadcastcate(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
        ]);
        $classificationDictionary =new ClassificationDictionaryLogic();
        $name ='classification_name_'.$param['lang_code'];
        $filed = 'dictionary_id,'.$name.' as classification_name';
        $data =$classificationDictionary->where(['classification_type'=>18])->field($filed)->select();
        # 在开头添加一个新元素 [0, 0]
        $data =$data->toArray();
        $new_element['dictionary_id']=0;
        $new_element['classification_name']='全部';

        array_unshift($data, $new_element);
        return $this->success(['data'=>$data]);
    }

    /**
     * 直播介绍
     */
    #[LoginRequired(required:false)]
    public function  livebroadcastinfo(Request $request){
        $param = $request->more(
            ['broadcast_id',0],
        );
        $liveBroadcast=new liveBroadcastLogic();
        $data=$liveBroadcast->where(['broadcast_id'=>$param['broadcast_id']])->field('broadcast_id,title,content,playback_url')->find();
        # 在开头添加一个新元素 [0, 0]
        $data =$data->toArray();
        return $this->success(['data'=>$data]);
    }
    /**
     * 直播列表
     */
    #[LoginRequired(required:false)]
    public function  livebroadcastlist(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
            ['keyword',''],
            ['cate',0],
            ['limit',10],
            ['page',1],
        ]);

        $liveBroadcast=new liveBroadcastLogic();
        $query=$liveBroadcast->search([
            'lang_code'=>$param['lang_code'],
        ]);
        if($param['keyword']){
            $query->where('enterprise_name|title|content', 'like', "%{$param['keyword']}%");
        }

        if($param['cate'] != 0){
            $where['cate']=$param['cate'];
            $where['broadcast_type'] =6;
            $order = 'sort_order asc';

        }else{
            $where['broadcast_type'] =5;
            $order = 'broadcast_id desc ';
        }

        $where['lang_code']=$param['lang_code'];
        $where['is_display'] =1;
        $liveBroadcastData=$query
            ->where($where)
            ->whereNull('delete_time')
            ->order('broadcast_id desc')
            ->order($order)
//            ->order('sort_order desc')
//            ->order('create_time desc')
            ->paginate($param['limit']);
        return $this->success(['data'=>$liveBroadcastData->items(),'total'=>$liveBroadcastData->total()]);
    }


    /**
     * 直播列表
     */
    #[LoginRequired(required:false)]
    public function  livelist(Request $request){
        $param = $request->more([
            ['lang_code','zh'],
//            ['keyword',''],

//            ['cate',0],
//            ['limit',10],
//            ['page',1],
        ]);

        $liveBroadcast=new liveBroadcastLogic();
        $query=$liveBroadcast->search([
            'lang_code'=>$param['lang_code'],
        ]);
//        if($param['keyword']){
//            $query->where('enterprise_name|title|content', 'like', "%{$param['keyword']}%");
//        }

//        if($param['cate'] == 0){
//            $where['cate']=$param['cate'];
            $where['broadcast_type'] =58;
//        }

        $where['lang_code']=$param['lang_code'];
        $where['is_display'] =1;
        $liveBroadcastData=$query
            ->where($where)
            ->whereNull('delete_time')
            ->order('sort_order desc')
            ->order('create_time desc')
            ->select();
        return $this->success(['data'=>$liveBroadcastData]);
    }

    /**获取下一个视频
     * @param Request $request
     * @return Response
     */
    #[LoginRequired(required: false)]
    public function livenext(Request $request){
        $param = $request->more([
            ['lang_code', 'zh'],
            ['broadcast_id', 0], // 接收当前视频的ID
            ['direction',1] // 1、下一条 2、上一条
        ]);

        $liveBroadcast = new liveBroadcastLogic();
        $query = $liveBroadcast->search([
            'lang_code' => $param['lang_code'],
        ]);

        $where['lang_code'] = $param['lang_code'];
        $where['is_display'] = 1;
        $where['broadcast_type'] =5;

        if ($param['direction'] == 1) { // 下一条
            $where[] = ['broadcast_id', '<', $param['broadcast_id']];
            $order = 'broadcast_id desc';
        } else { // 上一条
            $where[] = ['broadcast_id', '>', $param['broadcast_id']];
            $order = 'broadcast_id asc';
        }

        $liveBroadcastData = $query
            ->where($where)
            ->whereNull('delete_time')
            // 按ID升序排序，取第一条就是当前ID的下一条
            ->order($order)
            ->find(); // 使用find()获取单条数据

        return $this->success(['data' => $liveBroadcastData]);
    }

    /**
     * 直播点击加1
     */
    #[LoginRequired(required:false)]
    public function setint(Request $request){
        $param = $request->more(
          ['broadcast_id',0],
        );

        if($param['broadcast_id'] == 0){
            return  $this->fail('缺少参数');
        }
        $liveBroadcast=new liveBroadcastLogic();
        $liveBroadcastData=$liveBroadcast->where(['broadcast_id'=>$param['broadcast_id']])->setinc('num');
        return $this->success('成功');
    }



}